'- from Paulo Silva, GPL license, december '10
setdisplay(256,256,32,1):paper(8^8-1):ink(0):pen(0):cls
dim plte[4]:plte[0]=0x000000:plte[1]=0x0000FF:plte[2]=0xFF0000:plte[3]=0x00FF00
dim clust[8,8]:a$="00,06,08,14,02,12,04,10,08,14,00,06,04,10,02,12"
for y2=0 to 3:for x2=0 to 3:lc=(y2*4)+x2:clust[x2,y2]=val(mid$(a$,(lc*3)+1,2)):next:next
captool$="fswebcam"
fout$="_.bin"
while 0=0
  if captool$="vgrabbj" then
    setcaption("dummy")
    shell("vgrabbj -d /dev/video0 -F 4 -f _1.jpg")
    setcaption("del")
    shell("rm _1.jpg")
    setcaption("webcam")
    shell("vgrabbj -d /dev/video0 -F 4 -f _1.jpg")
    end if
  if captool$="fswebcam" then
    setcaption("del")
    shell("rm _1.jpg")
    setcaption("webcam")
    shell("fswebcam -d /dev/video0 _1.jpg")
    end if
  setcaption("imagemagick")
  shell("convert _1.jpg -resize 256x256\\! _2.jpg")  '- shell("convert _tmpr1.jpg -resize 256x256\\! -level 0,80%,1 _tmpr2.jpg")
  setcaption("del")
  shell("rm _1.jpg")
  setcaption("load")
  loadimage("_2.jpg",1):pasteicon(0,0,1)
  setcaption("del")
  shell("rm _2.jpg")
  setcaption("conv")
  '---
  for y=0 to 255
    for x1=0 to 63
      btv=0:btt=0
      for x2=3 to 0 step -1
        x=x1*4+(3-x2)
        btv=btv*4
        u=point(x,y)
        b=u mod 256: u=int(u/256)
        g=u mod 256: u=int(u/256)
        r=u mod 256: u=int(u/256)
        w=int(((b*11)+(r*30)+(g*59))/100)
        if w<42 then:
          w2=int((w*255)/42):ip=0:ii=1:end if
        if w>=42 and w<128 then:
          w2=int(((w-42)*255)/86):ip=1:ii=2:end if
        if w>=128 then:
          w2=int(((w-128)*255)/128):ip=2:ii=3:end if
        patgf=(((clust[x mod 4,y mod 4]+1)*255)/16)
        btt=0:cvl=ip:if w2>patgf then:btt=1:cvl=ii:end if
        ink(plte[cvl])
        dot(x,y)
        btv=btv+cvl
        next
      'writebyte(1,btv)
      next:next
  grab(1,0,0,256,256):saveimage("_.bmp",1):shell("convert _.bmp _.png && rm _.bmp")
  'close #1
  setcaption("bin2cas...")
  '-????
  setcaption(".cas log...")
  '-????
  setcaption("cas2wav...")
  '-????
  setcaption("sox play...")
  '-????
  setcaption("erasing temporary files...")
  '-????
  wend
